package com.jf.exam.controller.teacher;

import com.jf.exam.pojo.data.TeacherDO;
import com.jf.exam.pojo.vo.*;
import com.jf.exam.service.QuestionService;
import com.jf.exam.utils.PageBean;
import com.jf.exam.utils.QuestionType;
import com.jf.exam.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;

@Controller
@RequestMapping("/teacher/question")
public class TeacherQuestionController {

    @Value("#{properties['question.pageSize']}")
    private int pageSize;

    @Value("#{properties['question.navigatePages']}")
    private int navigatePages;

    @Autowired
    QuestionService questionService;

    /**
     * 查询单选题无条件无页码
     */
    @RequestMapping("/singles")
    public String singlesHelper(Model model, HttpServletRequest request) throws Exception {
        model.addAttribute("function", 4);
        QuestionChoiceVO query = new QuestionChoiceVO();
        return singles(1, query, model, request);
    }

    /**
     * 查询单选题有页码
     */
    @RequestMapping("/singles/{pn}")
    public String singles(@PathVariable Integer pn, QuestionChoiceVO query, Model model, HttpServletRequest request) throws Exception {
        TeacherDO teacherDO = (TeacherDO) request.getSession().getAttribute("teacher");
        query.setPageCode(pn);
        query.setType(QuestionType.SINGLE);
        query.setFkTeacher(teacherDO.getId());
        model.addAttribute("type", QuestionType.SINLE_EN);
        model.addAttribute("function", 4);
        return questionChoice(query, model);
    }

    /**
     * 查询多选题无条件无页码
     */
    @RequestMapping("/multis")
    public String multisHelper(Model model, HttpServletRequest request) throws Exception {
        model.addAttribute("function", 5);
        QuestionChoiceVO query = new QuestionChoiceVO();
        return multis(1, query, model, request);
    }

    /**
     * 查询多选题有页码
     */
    @RequestMapping("/multis/{pn}")
    public String multis(@PathVariable Integer pn, QuestionChoiceVO query, Model model, HttpServletRequest request) throws Exception {
        TeacherDO teacherDO = (TeacherDO) request.getSession().getAttribute("teacher");
        query.setFkTeacher(teacherDO.getId());
        query.setPageCode(pn);
        query.setType(QuestionType.MULTI);
        model.addAttribute("type", QuestionType.MULTI_EN);
        model.addAttribute("function", 5);
        return questionChoice(query, model);
    }

    //添加查询选择题的方法
    public String questionChoice(QuestionChoiceVO query, Model model) throws Exception {
        query.setSize(navigatePages);
        query.setPageSize(pageSize);
        PageBean<QuestionListVO> pageBean = questionService.listQuestionChoice(query);
        model.addAttribute("pageBean", pageBean);
        return "teacher/question_list";
    }


    /**
     * 查询判断题无条件无页码
     */
    @RequestMapping("/judges")
    public String judgesHelper(Model model, HttpServletRequest request) throws Exception {
        model.addAttribute("function", 6);
        QuestionJudgeVO query = new QuestionJudgeVO();
        return judges(1, query, model, request);
    }

    /**
     * 查询单选题有页码
     */
    @RequestMapping("/judges/{pn}")
    public String judges(@PathVariable Integer pn, QuestionJudgeVO query, Model model, HttpServletRequest request) throws Exception {
        TeacherDO teacherDO = (TeacherDO) request.getSession().getAttribute("teacher");
        query.setPageCode(pn);
        query.setSize(navigatePages);
        query.setPageSize(pageSize);
        query.setType(QuestionType.JUDGE);//判断题在单独的表里面，无需设置
        query.setFkTeacher(teacherDO.getId());
        PageBean<QuestionListVO> pageBean = questionService.listQuestionJudge(query);
        model.addAttribute("pageBean", pageBean);
        model.addAttribute("type", QuestionType.JUDGE_EN);
        model.addAttribute("function", 6);
        return "teacher/question_list";
    }

    /**
     * 添加、更新题目
     * 如果id为-1 表示是添加
     * 如果id不是-1 表示是更新操作
     * <p>
     * 在试卷管理页面添加的题目
     * 1. 先插入题目信息
     * 2. 在插入和试卷的关系
     * 3. 更新试卷的分数信息
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(QuestionAddVO questionAddVO, String examId, HttpServletRequest request) throws Exception {
        System.out.println(examId);
        //添加题目需要记录是哪个教师添加的
        TeacherDO teacherDO = (TeacherDO) request.getSession().getAttribute("teacher");
        //判断题
        if (questionAddVO.getType().equals(QuestionType.JUDGE_EN)) {
            QuestionJudgeVO vo = new QuestionJudgeVO();
            vo.setId(null);
            vo.setFkTeacher(teacherDO.getId());
            vo.setAnswer(questionAddVO.getAnswer());
            vo.setScore(new BigDecimal(questionAddVO.getPoint()));
            vo.setQuestion(questionAddVO.getTitle());
            vo.setType(QuestionType.JUDGE);
            vo.setDelFalg(0);
            if (questionAddVO.getId() == -1 || questionAddVO.getId() == 0) {
                //添加的是判断题
                return questionService.addQuestionJudge(vo, examId);
            } else {
                //新添加的题目，需要记录是哪个教师添加的
                vo.setId(questionAddVO.getId());
                //更新
                return questionService.updateQuestionJudge(vo);
            }
        } else {
            QuestionChoiceVO vo = new QuestionChoiceVO();
            vo.setQuestion(questionAddVO.getTitle());
            vo.setOptiona(questionAddVO.getOptionA());
            vo.setOptionb(questionAddVO.getOptionB());
            vo.setOptionc(questionAddVO.getOptionC());
            vo.setOptiond(questionAddVO.getOptionD());
            vo.setAnswer(questionAddVO.getAnswer());
            vo.setScore(new BigDecimal(questionAddVO.getPoint()));
            //判断题目的类型
            if (questionAddVO.getType().equals(QuestionType.SINLE_EN)) {
                vo.setType(QuestionType.SINGLE);//单选题
            } else {
                vo.setType(QuestionType.MULTI);//多选题
            }
            if (questionAddVO.getId() == -1 || questionAddVO.getId() == 0) {
                //添加新的题目
                vo.setFkTeacher(teacherDO.getId());
                return questionService.addQuestionChoice(vo, examId);
            } else {
                //新添加的题目，需要记录是哪个教师添加的
                //更新
                vo.setId(questionAddVO.getId());
                return questionService.updateQuestionChoice(vo);
            }
        }
    }

    /**
     * @RequestBody 获取请求体的内容
     */
    @RequestMapping("/repository")
    @ResponseBody
    public Result repository(@RequestBody QuestionBankVO vo) throws Exception {
//        {"types":[{"id":"26","type":"1"},{"id":"27","type":"2"},{"id":"28","type":"1"}
//        ,{"id":"29","type":"2"},{"id":"30","type":"1"}],"type":"1"}
        return questionService.listAllByType(vo);
    }

    /**
     * @RequestBody 获取请求体的内容
     */
    @RequestMapping("/addExamQuestion")
    @ResponseBody
    public Result addExamQuestion(@RequestBody QuestionBankVO vo) throws Exception {
        //{"types":[{"id":"1","point":"10"},{"id":"21","point":"1"}],"examId":"1","type":"1"}
        System.out.println(vo);
        //构造试卷和题目关系的对象
        ArrayList<ExamQuestionVO> examQuestionVOS = new ArrayList<>();
        for (TypesVO typesVO : vo.getTypes()) {
            ExamQuestionVO examQuestionVO = new ExamQuestionVO();
            //题目类型，在更新试卷信息时用到
            examQuestionVO.setFkQtype(Integer.parseInt(vo.getType()));
            examQuestionVO.setFkExam(Integer.parseInt(vo.getExamId()));
            examQuestionVO.setFkQuestion(Integer.parseInt(typesVO.getId()));
            //题目分数，在更新试卷信息时用到
            examQuestionVO.setScore(Integer.parseInt(typesVO.getPoint()));
            examQuestionVOS.add(examQuestionVO);
        }
        return questionService.addExamQuestion(examQuestionVOS, Integer.parseInt(vo.getExamId()));
    }


    /**
     * @RequestBody 获取请求体的内容
     */
    @RequestMapping("/rate/{qid}")
    @ResponseBody
    public Result rate(@PathVariable Integer qid) throws Exception {
        return questionService.rate(qid);
    }


}
